package com.qq.first_project.controller.common;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qq.first_project.common.R;
import com.qq.first_project.entity.Picture;
import com.qq.first_project.service.PictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Qiu
 * @since 2022-06-10
 */
@RestController
@RequestMapping("/common/picture")
@CrossOrigin //跨域
@Slf4j
public class PictureController {

    @Autowired
    private PictureService pictureService;

    /**
     * 客户端查询所有图片
     */
    @GetMapping("/list")
    R selectPictures(){
        List<Picture> pictureList =pictureService.list();
        return R.ok().data("data",pictureList).message("获取图片成功");
    }

    /**
     * 管理端端查询所有图片
     */
    @GetMapping("/page-list")
    R listAll(ModelMap modelMap, @RequestParam("current") int num, @RequestParam("pageSize") int size, String name ) {
        Page<Picture> page = new Page<>(num,size);
        IPage<Picture> list = pictureService.pageList(page,name);
        modelMap.addAttribute("page",list);
        return R.ok().data("data",list).message("获取成功");
    }

    /**
     * 图片上传
     * @param multipartFile 上传的图片
     * @param picture 表实体类
     * @return 成功 or 失败的信息
     */
    @PostMapping("/upload")
    R uploadPicture(@RequestParam("picture") MultipartFile multipartFile, Picture picture){
        int result = pictureService.uploadPicture(multipartFile,picture);
        //判断是否执行成功
        if (result > 0){
            return R.ok().message("上传成功");
        }else {
            return R.error().message("上传失败");
        }
    }

    /**
     * 展示所有图片
     */
    @GetMapping("/getPicturePaths")
    R getPicturePaths(){
        List<Picture> pictures= pictureService.list();
        ArrayList<String> paths = new ArrayList<>();
        if(pictures!=null && !pictures.isEmpty()){
            for(Picture i:pictures){
                paths.add(i.getPath());
            }
        }
        return R.ok().data("data",paths).message("获取图片成功");
    }

    /**
     * 新增
     * @param picture
     * @return
     */
    @PostMapping("save")
    R save(Picture picture){
        boolean result = pictureService.save(picture);
        if (result){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }

    /**
     * 修改
     * @param picture
     * @return
     */
    @PostMapping("updateById")
    R updateById(Picture picture){
        boolean result = pictureService.updateById(picture);
        if (result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("delById")
    R deleteById(Long id){
        boolean result = pictureService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }
}
